package org.linloong.modules.apis.common.controller;// AuthController.java
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.nls.client.AccessToken;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.linloong.core.result.Result;
import org.linloong.modules.apis.common.dto.UserLoginDTO;
import org.linloong.modules.apis.common.dto.UserRegisterDTO;
import org.linloong.modules.apis.common.entity.User;
import org.linloong.modules.apis.common.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/auth")
@Tag(name = "认证模块", description = "用户注册、登录、登出等操作")
public class AuthController {
    
    @Autowired
    private UserService userService;

    @Value("${ali.accessKey}")
    private String accessKey;


    @Value("${ali.accessSecret}")
    private String accessSecret;
    
    /**
     * 用户注册
     */
    @SaIgnore
    @PostMapping("/register")
    public Result register(@Valid @RequestBody UserRegisterDTO userRegisterDTO) {
        boolean result = userService.register(userRegisterDTO);
        return Result.success(result ? "注册成功" : "注册失败");
    }
    
    /**
     * 用户登录
     */
    @SaIgnore
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录，返回登录凭证")
    public Result<SaTokenInfo> login(@Valid @RequestBody UserLoginDTO userLoginDTO) {
        SaTokenInfo tokenInfo = userService.login(userLoginDTO);
        return Result.success(tokenInfo);
    }

    @GetMapping("/mockLogin")
    @Operation(summary = "模拟用户登录", description = "用户登录，返回登录凭证")
    public Result<SaTokenInfo> mockLogin() {
        SaTokenInfo tokenInfo = userService.login(new UserLoginDTO("admin","admin"));
        return Result.success(tokenInfo);
    }
    
    /**
     * 用户登出
     */
    @PostMapping("/logout")
    public Result logout() {
        userService.logout();
        return Result.success("登出成功");
    }
    
    /**
     * 获取当前用户信息
     */
    @GetMapping("/info")
    public Result<User> getUserInfo() {
        Long userId = StpUtil.getLoginIdAsLong();
        User user = userService.getById(userId);
        user.setPassword(null); // 不返回密码
        return Result.success(user);
    }

    @Operation(summary = "获取阿里语音转文字token")
    @GetMapping("/getAliToken")
    public Result<Map<String,Object>> getAliToken() throws IOException {
        AccessToken accessToken = new AccessToken(accessKey,accessSecret);
        accessToken.apply();
        String token = accessToken.getToken();
        long expireTime = accessToken.getExpireTime();
        Map<String,Object>  map = new HashMap<>();
        map.put("token",token);
        map.put("expireTime",expireTime);
        return Result.success(map);
    }
}
